package com.dream.keigezhushou.Activity.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.SingInfo;
import com.dream.keigezhushou.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;

/**
 * Created by kaifa on 2017/2/24.
 */
public class SingFragment extends Fragment implements View.OnClickListener {
    private ArrayList<SingInfo> mList = new ArrayList<>();
    private PullToRefreshListView mMPullToRefreshListView;
    private View mHeadView;
    private ListView mListlv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        //getDataFromService();   获取网络数据并解析
        // 加载动画
        //设置上拉刷新重新请求网络数据  更新adapter?
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mMPullToRefreshListView = (PullToRefreshListView) UiUtils.inflateView(R.layout.singfragmentlayout);
        mHeadView = UiUtils.inflateView(R.layout.singfragment_head);
        mListlv = mMPullToRefreshListView.getRefreshableView();
        mListlv.addHeaderView(mHeadView);


        ImageView sing = (ImageView) mHeadView.findViewById(R.id.sing_image_bg);
        sing.setOnClickListener(this);
        TextView mine = (TextView) mHeadView.findViewById(R.id.mine);
        mine.setOnClickListener(this);
        TextView hand_sowing = (TextView) mHeadView.findViewById(R.id.hand_sowing);
        hand_sowing.setOnClickListener(this);
        TextView oratorio = (TextView) mHeadView.findViewById(R.id.oratorio);
        oratorio.setOnClickListener(this);
        TextView viewById = (TextView) mHeadView.findViewById(R.id.chorus);
        viewById.setOnClickListener(this);
        TextView song_helper = (TextView) mHeadView.findViewById(R.id.song_helper);
        song_helper.setOnClickListener(this);
        TextView hot_recommend = (TextView)mHeadView.findViewById(R.id.hot_recommend);
        hot_recommend.setOnClickListener(this);
        TextView my_attention = (TextView)mHeadView.findViewById(R.id.my_attention);
        my_attention.setOnClickListener(this);




        mListlv.setAdapter(new MyAdapter(mList));

              return mMPullToRefreshListView;

    }

    private void initData() {
        for (int i=0;i<6;i++){
            SingInfo info = new SingInfo();
            info.setIv(getResources().getDrawable(R.mipmap.my_attention));
            info.setName("白色气球");
            info.setAuthor("周杰伦");
            info.setNumber("198万人唱过");
            mList.add(info);

        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sing_image_bg:
                UiUtils.toast("跳转页面");
            break;
            case R.id.mine:
                UiUtils.toast("跳转页面");
            break;
            case R.id.hand_sowing:
                UiUtils.toast("跳转页面");
            break;
            case R.id.oratorio:
                UiUtils.toast("跳转页面");
            break;
            case R.id.chorus:
                UiUtils.toast("跳转页面");
            break;
            case R.id.song_helper:
                UiUtils.toast("跳转页面");
            break;
            case R.id.hot_recommend:
                UiUtils.toast("跳转页面");
            break;
            case R.id.my_attention:
                UiUtils.toast("跳转页面");
            break;


        }

    }

    private class MyAdapter extends BaseAdapter {
        private  ArrayList<SingInfo> mList;

        public MyAdapter(ArrayList<SingInfo> list) {
            this.mList = list;

        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public SingInfo getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if(convertView==null){
                convertView=UiUtils.inflateView(R.layout.item_singfragment);
                holder=new ViewHolder();
                holder.iv= (ImageView) convertView.findViewById(R.id.icon_iv);
                holder.name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.author = (TextView)convertView.findViewById(R.id.author);
                holder.number = (TextView) convertView.findViewById(R.id.number);
                holder.tv = (TextView) convertView.findViewById(R.id.tv_kg);
                convertView.setTag(holder);

            }else{
                holder= (ViewHolder) convertView.getTag();
            }
            SingInfo info = getItem(position);
            holder.iv.setImageDrawable(info.getIv());
            holder.name.setText(info.getName());
            holder.author.setText(info.getAuthor());
            holder.number.setText(info.getNumber());

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UiUtils.toast("开始K歌");
                    //跳K歌页面
                }
            });
            return convertView;
        }
    }
    static class ViewHolder{
         ImageView iv;
         TextView name;
        TextView author;
        TextView number;
        TextView tv;
    }
}
